﻿Ext.define('app.system.role.RoleMember',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    width: 800,
    height: 600,
    layout: 'fit',
    roleId: null,
    bodyStyle: 'padding:5px',
    initComponent: function () {
        var me = this;
        me.items = [me.getUserList()];
        me.buttons = [
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    getUserList: function () {
        var me = this;
        if (!this.userList) {


            var txt_account = new Ext.form.field.Text({ emptyText: '账号' });
            var txt_name = new Ext.form.field.Text({ emptyText: '姓名' });

            this.userList = new Ext.grid.Panel({
                tbar: [
                    txt_account,
                    "&nbsp;-&nbsp;",
                    txt_name,
                    {
                        xtype: 'button', text: '查询', handler: function () {
                            var account = txt_account.getValue();
                            var name = txt_name.getValue();
                              me.userList.store.clearFilter();
                           
                            if (account && account != "") {
                                me.userList.store.filterBy(function (record) { return record.data.userName.indexOf(account) != -1; });
                            }
                            if (name && name != "") {
                                me.userList.store.filterBy(function (record) { return record.data.name.indexOf(name) != -1; });
                            }
                        }
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                multiColumnSort: true,
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetAll', filter: {},remoteSort:false }),
                columns: [
                    { header: '账号', dataIndex: 'userName' },
                    { header: '姓名', dataIndex: 'name', sortable: false },
                    {
                        header: '角色', dataIndex: 'roles', sortable:false,renderer: function (value, p, record) {
                            var roleNames = '';
                            for (var j = 0; j < value.length; j++) {
                                if (roleNames.length) {
                                    roleNames = roleNames + ', ';
                                }

                                roleNames = roleNames + value[j].roleName;
                            };

                            return roleNames;
                        }
                    },
                    {
                        xtype: 'actioncolumn',
                        width: 35,
                        header: localize('Operating'),
                        items: [
                        {
                            icon: '/Content/images/accept.png',
                            tooltip: '选择',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                App.AjaxPost(
                                    '/api/services/app/user/SetUserRole',
                                    { UserId: record.data.id, RoleId: me.roleId },
                                    function (data) {
                                        
                                        me.fireEvent("savesuccess");
                                    });
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.userList;
    }
});